from common.Base import Base, open_op_browser, open_browser
from time import sleep

login_url = "http://localhost:8080/ecshop/user.php"
path = r"C:\Users\GrapeLier\AppData\Roaming\Mozilla\Firefox\Profiles\iybiyjzp.default-release"


class AddressPage(Base):
    addr_loc = ("link text", "收货地址")
    country_loc = ("id", "selCountries_0")
    province_loc = ("id", "selProvinces_0")
    city_loc = ("id", "selCities_0")
    district_loc = ("id", "selDistricts_0")
    consignee_loc = ("id", "consignee_0")
    addr_email_loc = ("id", "email_0")
    address_loc = ("id", "address_0")
    zip_code_loc = ("id", "zipcode_0")
    phone_loc = ("id", "tel_0")
    tel_loc = ("id", "mobile_0")
    add_address_loc = ("name", "submit")
    del_info_loc = ("name", "button")

    # 以下的定位仅限于 操作修改第一个地址 或者 第一次添加地址
    def click_address(self):
        """
        定位修改地址选项
        :return:
        """
        self.click(self.addr_loc)

    def click_country(self, country):
        """
        定位选择国家
        :param country:
        :return:
        """
        self.select_by_text(self.country_loc, country)

    def click_province(self, province):
        """
        定位选择省份
        :param province:
        :return:
        """
        self.select_by_text(self.province_loc, province)

    def click_city(self, city):
        """
        定位选择城市
        :param city:
        :return:
        """
        self.select_by_text(self.city_loc, city)

    def click_district(self, district):
        """
        定位选择区域
        :param district:
        :return:
        """
        self.select_by_text(self.district_loc, district)

    def input_consignee(self, consignee):
        """
        定位输入收货人
        :param consignee:
        :return:
        """
        self.clear_send_text(self.consignee_loc, consignee)

    def input_addr_email(self, addr_email):
        """
        定位输入电子邮箱地址
        :param addr_email:
        :return:
        """
        self.clear_send_text(self.addr_email_loc, addr_email)

    def input_address(self, addr):
        """
        定位输入收货详细地址
        :param addr:
        :return:
        """
        self.clear_send_text(self.address_loc, addr)

    def input_zip_code(self, zip_code):
        """
        定位邮编地址
        :param zip_code:
        :return:
        """
        self.clear_send_text(self.zip_code_loc, zip_code)

    def input_phone(self, phone):
        """
        定位电话
        :param tel:
        :return:
        """
        self.clear_send_text(self.phone_loc, phone)

    def input_tel(self, tel):
        """
        定位手机号
        :param tel:
        :return:
        """
        self.clear_send_text(self.tel_loc, tel)

    def add_address(self):
        """
        定位新增地址
        :return:
        """
        self.click(self.add_address_loc)

    def del_info(self):
        """
        定位删除地址
        :return:
        """
        self.click(self.del_info_loc)


if __name__ == '__main__':
    # 打开浏览器
    driver = open_op_browser(path)
    ap = AddressPage(driver)
    # 打开网页
    ap.open_url(login_url)
    # 点击收货地址进入收货地址页面
    ap.click_address()
    # 定位国家
    ap.click_country("中国")
    # 定位省份
    ap.click_province("河北省")
    sleep(2)
    # 定位城市
    ap.click_city("唐山市")
    # 定位区域
    sleep(5)
    ap.click_district("路北区")
    # 定位输入收货人
    ap.input_consignee("王小明")
    # 定位输入邮箱地址
    ap.input_addr_email("123@qq.com")
    # 定位 输入详细地址
    ap.input_address("详细地址")
    # 定位邮政编码
    ap.input_zip_code("28724")
    # 定位电话
    ap.input_phone("82112673")
    # 定位手机号
    ap.input_tel("15180600331")
    # 定位新增收货地址按钮
    ap.add_address()